package com.aismy.admin.controller;

import com.aismy.admin.entity.EduResource;
import com.aismy.admin.service.EduResourceService;

import com.aismy.admin.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/admin/resource")
public class EduResourceController {

    @Autowired
    private EduResourceService eduResourceService;

    /**
     * 分页查询资源列表
     */
    @GetMapping("/list")
    public Result<Page<EduResource>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String type) {
        Page<EduResource> page = eduResourceService.listResources(current, size, title, type);
        return Result.success(page);
    }

    /**
     * 获取资源详情
     */
    @GetMapping("/{id}")
    public Result<EduResource> getById(@PathVariable Long id) {
        return Result.success(eduResourceService.getById(id));
    }

    /**
     * 新增资源
     */
    @PostMapping
    public Result<Void> add(@RequestBody EduResource resource) {
        eduResourceService.save(resource);
        return Result.success();
    }

    /**
     * 更新资源
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody EduResource resource) {
        resource.setId(id);
        eduResourceService.updateById(resource);
        return Result.success();
    }

    /**
     * 删除资源
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        eduResourceService.removeById(id);
        return Result.success();
    }

    /**
     * 更新资源状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        eduResourceService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 上传视频
     */
    @PostMapping("/upload")
    public Result<String> uploadVideo(@RequestParam("file") MultipartFile file) {
        String url = eduResourceService.uploadVideo(file);
        return Result.success(url);
    }

    /**
     * 增加浏览量
     */
    @PostMapping("/{id}/view")
    public Result<Void> incrementViewCount(@PathVariable Long id) {
        eduResourceService.incrementViewCount(id);
        return Result.success();
    }
} 